package com.xiaotu.spider.project.pageprocessor;

import com.xiaotu.common.exception.SpiderException;
import com.xiaotu.common.exception.SpiderExceptionCode;
import com.xiaotu.common.util.*;
import com.xiaotu.spider.AUrlPageProcessor;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.selector.Html;

import java.util.List;

/**
 * 爬取中国道具网的道具信息
 */
@Component("ChinaPropsPageProcessor")
public class ChinaPropsPageProcessor extends AUrlPageProcessor
{

    private static final String URL = "http://www.chinaprops.com";

    private static final String TABLE_HTML = "<table><tbody>%s</tbody></table>";

    @Override
    public void process(Page page)
    {
        if (URL.equals(page.getUrl().toString()))
            this.listProcess(page);
        else
            this.detailProcess(page);
    }

    private void listProcess(Page page)
    {
        String listTable = page.getHtml().xpath
                ("//td[@class='tdbg_left']/table[2]/tbody/tr/td/table/tbody/tr/td/table").get();
        if (StringUtils.isEmpty(listTable))
            throw new SpiderException(SpiderExceptionCode.DataInvalideException);
        List<String> trList = new Html(listTable).xpath("/html/body/table/tbody/tr").all();
        if (trList == null || trList.isEmpty())
            throw new SpiderException(SpiderExceptionCode.DataInvalideException);

        Html parentObj = null;
        for (String trStr : trList)
        {
            Html itemObj = new Html(String.format(TABLE_HTML, trStr));
            if (itemObj.xpath("//tr/td/@class").get().contains("SortEndFolder"))//含有子节点的父节点
                parentObj = itemObj;
            else if (itemObj.xpath("//tr/td/@class").get().contains("SortFile"))//无子节点的父节点
            {
                page.getRequest().putExtra("root", itemObj.xpath("//a/text()").get());
                page.getRequest().getExtras().remove("second");
                PageUtils.addTargetRequest(page, getEncodeUrl(itemObj.xpath("//a/@href").get()), page.getRequest()
                        .getExtras(), true);
            }
            else//子节点
            {
                page.getRequest().putExtra("root", parentObj.xpath("//a/text()").get());
                List<String> childList = itemObj.xpath("/html/body/table/tbody/tr/td/table/tbody/tr").all();
                if (childList == null || childList.isEmpty())
                    throw new SpiderException(SpiderExceptionCode.DataInvalideException);
                childList.forEach(childStr ->
                {
                    Html childObj = new Html(childStr);
                    page.getRequest().putExtra("second", childObj.xpath("//a/text()").get());
                    PageUtils.addTargetRequest(page, getEncodeUrl(childObj.xpath("//a/@href").get()), page.getRequest
                                    ().getExtras()
                            , true);
                });
            }

        }

    }

    private String getEncodeUrl(String url)
    {
        String chn = RegexUtils.regexSearch(RegexUtils.REGEX_CH, url);
        if (StringUtils.isEmpty(chn))
            return url;
        return url.replaceAll(RegexUtils.REGEX_CH, KeywordEncode.encode(chn));
    }

    private void detailProcess(Page page)
    {
        String tableStr = page.getHtml().xpath("//form[@action='Payment.asp']//td[@colspan='3']/table").get();
        if (StringUtils.isEmpty(tableStr))
            throw new SpiderException(SpiderExceptionCode.DataInvalideException);
        List<String> tableList = new Html(tableStr).xpath("/html/body/table/tbody/tr/td/table").all();
        if (tableList.isEmpty())
            throw new SpiderException(SpiderExceptionCode.DataInvalideException);
        tableList.forEach(tabStr ->
        {
            Html tabObj = new Html(tabStr);
            String name = tabObj.xpath("//td/a/text()").get();
            String imgUrl = tabObj.xpath("//img[@onload='javascript:DrawImage(this);']/@src").get();
            String url = tabObj.xpath("//td/a/@href").get();
            String code = RegexUtils.regexSearch("<td>(" + RegexUtils.REGEX_INTEGER + ")</td>", tabStr);
            code = RegexUtils.replaceStr(code, "<td>|</td>", StringUtils.EMPTY);
            String line = StringUtils.join(new String[]{StringUtil.nullToString(page.getRequest().getExtra("root")),
                            StringUtil.nullToString(page.getRequest().getExtra("second")), name, code, url, imgUrl},
                    SepratorUtil.SEP_CSV_FIELDS);
            System.out.println(line);
            FileUtil.writeFile("D://props.csv", line + SepratorUtil.LINE_SEPRATOR);
        });

        List<String> aList = page.getHtml().xpath("//a[@href]").all();
        aList.stream().filter(aStr -> "下一页".equals(new Html(aStr).xpath("//a/text()").get())).forEach(aStr ->
                PageUtils.addTargetRequest(page, new Html(aStr).xpath("//a/@href").get(), page.getRequest()
                        .getExtras()));
    }

    @Override
    protected String[] getStartRequestUrls()
    {
        return new String[]{URL};
    }

    @Override
    protected String getSpiderType()
    {
        return "ChinaProps";
    }

    @Override
    public String getPageDataType(Page page)
    {
        return "ChinaProps";
    }
}
